/*
 * FPServlet.java
 * 
 * Created by demory on Jun 15, 2008, 7:36:06 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.web.servlets;

import java.io.File;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.servlet.http.HttpServlet;
import org.fpdev.apps.web.FPServer;

/**
 * The base class for all Five Points servlets. Defines general shared
 * functionality such as logging and connecting to the FPServer via RMI.
 * 
 * @author demory
 */
public abstract class FPServlet extends HttpServlet {

  protected Logger mainLogger_; // the main logger
  private Logger queryLogger_; // a logger specific to this servlet and day
  private int currentLoggerDay_; // an int (days after 1/1/2000) 
  protected FPServer server_; // the server
  protected String homeDir_;
  protected int count_;

  @Override
  public void init() {
    try {

      // retreive the main logger created by the context listener
      mainLogger_ = Logger.getLogger("org.fpdev.tomcatLogger");

      // initialize the servlet-specific logger
      updateQueryLogger();

      count_ = 0;

      server_ = (FPServer) this.getServletContext().getAttribute("FPServer");
      homeDir_ = server_.get5PHome();
      mainLogger_.log(Level.INFO, "Servlet initialized: " + this.getClass().getName());
    } catch (RemoteException ex) {
      Logger.getLogger(FPServlet.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  protected Logger getQueryLogger() {
    Calendar now = Calendar.getInstance();
    int newDay = 1000 * (now.get(Calendar.YEAR) - 2000) + now.get(Calendar.DAY_OF_YEAR);
    if (newDay != currentLoggerDay_) {
      updateQueryLogger();
    }
    return queryLogger_;
  }

  private void updateQueryLogger() {
    try {

      // create/retrieve the logger
      queryLogger_ = Logger.getLogger("org.fpdev." + getLoggerCode() + "Logger");

      // remove handelers, if necessary
      if (queryLogger_.getHandlers().length > 0) {
        Handler[] handlers = queryLogger_.getHandlers();
        for (int i = 0; i < handlers.length; i++) {
          queryLogger_.removeHandler(handlers[i]);
        }
      }

      Calendar now = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("MMddyy");
      currentLoggerDay_ = 1000 * (now.get(Calendar.YEAR) - 2000) + now.get(Calendar.DAY_OF_YEAR);

      FileHandler handler = new FileHandler(homeDir_ + "server" + File.separator + "logs" + File.separator + getLoggerCode() + sdf.format(now.getTime()) + ".log", true);
      handler.setFormatter(new SimpleFormatter());
      queryLogger_.addHandler(handler);

    } catch (Exception ex) {
      Logger.getLogger(FPServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  protected abstract String getLoggerCode();
}
